package com.wwt.blog.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wwt.blog.mapper.CommentMapper;
import com.wwt.blog.pojo.Comment;
import com.wwt.blog.pojo.SysUser;
import com.wwt.blog.service.CommentService;
import com.wwt.blog.service.SysUserService;
import com.wwt.blog.service.ThreadService;
import com.wwt.blog.utils.UserThreadLocal;
import com.wwt.blog.vo.CommentVo;
import com.wwt.blog.vo.Result;
import com.wwt.blog.vo.UserVo;
import com.wwt.blog.vo.param.CommentParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImp implements CommentService {
    @Resource
    CommentMapper commentMapper;
    @Resource
    SysUserService sysUserService;
    @Autowired
    ThreadService threadService;
    @Override
    public Result getCommentsByArticleId(Long articleId) {
        LambdaQueryWrapper<Comment> wrapper=new LambdaQueryWrapper<>();
        wrapper.
                eq(Comment::getArticleId,articleId).
                eq(Comment::getLevel,1);
        List<Comment> comments = commentMapper.selectList(wrapper);
        List<CommentVo> commentVos = copyList(comments);

        return Result.result_success(commentVos);
    }

    @Override
    public Result comment(CommentParam commentParam) {
        //拿到当前用户
        SysUser sysUser = UserThreadLocal.get();
        Comment comment = new Comment();
        comment.setArticleId(commentParam.getArticleId());
        comment.setAuthorId(sysUser.getId());
        comment.setContent(commentParam.getContent());
        comment.setCreateDate(System.currentTimeMillis());
        Long parent = commentParam.getParent();
        if (parent == null || parent == 0) {
            comment.setLevel(1);
        }else{
            comment.setLevel(2);
        }
        //如果是空，parent就是0
        comment.setParentId(parent == null ? 0 : parent);
        Long toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        this.commentMapper.insert(comment);
        //评论完之后文章的评论数加一
        threadService.upDateComment(commentParam.getArticleId());
        return Result.result_success(null);

    }

    private List<CommentVo> copyList(List<Comment> comments){
        ArrayList<CommentVo> commentVos = new ArrayList<>();
        for (Comment commentVosTemp:comments) {
            commentVos.add(copy(commentVosTemp));
        }
        return commentVos;
    }
    private CommentVo copy(Comment comment){
        CommentVo commentVo = new CommentVo();
        //先复制相同元素 level createDate id content
        BeanUtils.copyProperties(comment,commentVo);
        commentVo.setId(String.valueOf(comment.getId()));
        //设置author作者
        UserVo userVo = sysUserService.findUserVoByAuthorId(comment.getAuthorId());
        commentVo.setAuthor(userVo);
        //设置子评论列表
        if(comment.getLevel()==1){
            List<CommentVo> childrensList = findChildrensByParentId(comment.getId());
            commentVo.setChildrens(childrensList);
        }
        //设置toUser
        if(comment.getLevel()>1){
            //查找给谁的评论
            UserVo  toUserVo= sysUserService.findUserVoByAuthorId(comment.getToUid());
            commentVo.setToUser(userVo);
        }
        return commentVo;
    }
    //根据parentId查找对应的子评论列表
    private List<CommentVo> findChildrensByParentId(Long parentId){
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper();
        wrapper.
                eq(Comment::getLevel,2).
                eq(Comment::getParentId,parentId);
        List<Comment> comments = commentMapper.selectList(wrapper);
        if(comments.isEmpty())
            return null;
        return copyList(comments);
    }
}
